<?php
/**
 * Controller_Ajax_Wysiwyg
 *
 * Класс Controller_Ajax_Wysiwyg отвечает за обработку запросов, свзяанных с WYSIWYG
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Controller_Ajax_Wysiwyg extends Controller_Ajax {
	public function actionSwitch() {
		switch ($this->arrRequest[2]) {
		  case 'upload': // Получение календаря для профайла гида
        $this->getActionUpload(); // POST
		    break;
		  case 'connector':
		  	$this->actionConnector();
			default: $this->show404Error();
		}
	}

	public function actionConnector() {
		Service_Debug::setDebug();
		$opts = array(
			'root'            => ABSPATH . 'content/files/wysiwyg',                       // path to root directory
			'URL'             => W_ABSPATH . 'content/files/wysiwyg/', // root directory URL
			'rootAlias'       => 'Home',       // display this instead of root directory name
			//'uploadAllow'   => array('images/*'),
			//'uploadDeny'    => array('all'),
			//'uploadOrder'   => 'deny,allow'
			// 'disabled'     => array(),      // list of not allowed commands
			// 'dotFiles'     => false,        // display dot files
			// 'dirSize'      => true,         // count total directories sizes
			// 'fileMode'     => 0666,         // new files mode
			// 'dirMode'      => 0777,         // new folders mode
			// 'mimeDetect'   => 'internal',       // files mimetypes detection method (finfo, mime_content_type, linux (file -ib), bsd (file -Ib), internal (by extensions))
			// 'uploadAllow'  => array(),      // mimetypes which allowed to upload
			// 'uploadDeny'   => array(),      // mimetypes which not allowed to upload
			// 'uploadOrder'  => 'deny,allow', // order to proccess uploadAllow and uploadAllow options
			// 'imgLib'       => 'mogrify',       // image manipulation library (imagick, mogrify, gd)
			// 'tmbDir'       => '.tmb',       // directory name for image thumbnails. Set to "" to avoid thumbnails generation
			// 'tmbCleanProb' => 1,            // how frequiently clean thumbnails dir (0 - never, 100 - every init request)
			// 'tmbAtOnce'    => 5,            // number of thumbnails to generate per request
			// 'tmbSize'      => 48,           // images thumbnails size (px)
			// 'fileURL'      => true,         // display file URL in "get info"
			// 'dateFormat'   => 'j M Y H:i',  // file modification date format
			// 'logger'       => null,         // object logger
			// 'defaults'     => array(        // default permisions
			// 	'read'   => true,
			// 	'write'  => true,
			// 	'rm'     => true
			// 	),
			// 'perms'        => array(),      // individual folders/files permisions    
			// 'debug'        => true,         // send debug to client
			// 'archiveMimes' => array(),      // allowed archive's mimetypes to create. Leave empty for all available types.
			// 'archivers'    => array()       // info about archivers to use. See example below. Leave empty for auto detect
			// 'archivers' => array(
			// 	'create' => array(
			// 		'application/x-gzip' => array(
			// 			'cmd' => 'tar',
			// 			'argc' => '-czf',
			// 			'ext'  => 'tar.gz'
			// 			)
			// 		),
			// 	'extract' => array(
			// 		'application/x-gzip' => array(
			// 			'cmd'  => 'tar',
			// 			'argc' => '-xzf',
			// 			'ext'  => 'tar.gz'
			// 			),
			// 		'application/x-bzip2' => array(
			// 			'cmd'  => 'tar',
			// 			'argc' => '-xjf',
			// 			'ext'  => 'tar.bz'
			// 			)
			// 		)
			// 	)
		);
		$fm = new Service_ElFinder($opts); 
    $fm->run();
	}
	
	/**
	 * Метод осуществляет загрузку изображения для WYSIWYG
	 *
	 */
	public function getActionUpload() {
    $this->usePlainText = true;
    $userId = User_User::getId();
		$arrPath = pathinfo($_FILES['file']['name']);
		try {
			$objImage = new Service_Image(array('throw' => true, 'folder' =>  'essence/wysiwyg/', 'usePrefix' => true));
			$objImage->loadScrAsArray($_FILES['file']);
			$arrSizes = array();
			$arrSizes[0] = array('width' => 50, 'height' => 50, 'prefix' => 's_');
			$arrSizes[1] = array('width' => 200, 'height' => 200, 'prefix' => 'm_');
			$arrSizes[2] = array('width' => 650, 'height' => 650, 'prefix' => 'b_');
			$objImage->setArrSizes($arrSizes);
			$objImage->save();
			$arrSizes = $objImage->getArrSizes();
			echo W_IMAGES . $arrSizes[1]['pic_folder'] . $arrSizes[1]['pic_file'];
			/*
			$image = new Image('file', $_FILES['file'], array('folder' => 'essence/wysiwyg/', 'deleteOnChange' => false), null);
			$image->addArrSize(0, 0, '');
			$image->addArrSize(50, 50, 's_');
			$image->addArrSize(650, 650, 'm_');
			$image->save();
			*/
		  //echo W_IMAGES . 'essence/wysiwyg/m_' . $image->getImageName();
		}
		catch(Exception $ex) {
		  echo $ex->getMessage();
		}
	}
}